package com.shuyu.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;

public final class DesCode {
    public static void main(String[] args) {
        System.out.println(encrypt("18123116789"));
    }

    private static Logger logger   = LoggerFactory.getLogger(DesCode.class);

    //密码，长度要是8的倍数
    private static String password="didolkey";
    /**
     *
     * @Method: encrypt
     * @Description: 加密数据
     * @param data
     * @return
     * @throws Exception
     */
    public static String encrypt(String data) {  //对string进行BASE64Encoder转换
        if(StringUtils.isEmpty(data)){
            return "";
        }
        byte[] bt = encryptByPwd(data.getBytes(), password);
        /*        BASE64Encoder base64en = new BASE64Encoder();
        String strs = base64en.encode(bt);
        return strs;
        */
        return Base64.encode(bt);

   

    }

    /**
     *
     * @Method: encrypt
     * @Description: 解密数据
     * @param data
     * @return
     * @throws Exception
     */
    public static String decryptor(String data) {  //对string进行BASE64Encoder转换
        /*            sun.misc.BASE64Decoder base64en = new sun.misc.BASE64Decoder();
        byte[] bt = decrypt(base64en.decodeBuffer(data), password);
        String strs = new String(bt);
        return strs;*/
        if(StringUtils.isEmpty(data)){
            return "";
        }
        if(data.equalsIgnoreCase("null")){
            return null;
        }
        try {
            byte[] bt = decrypt(Base64.decode(data), password);
            String strs = new String(bt);
            return strs;
        }catch (Exception e){
            return data;
        }

    }

    /**
     * 加密
     * @param datasource byte[]
     * @param password String
     * @return byte[]
     */
    public static byte[] encryptByPwd(byte[] datasource, String password) {
        try{
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(password.getBytes());
            //创建一个密匙工厂，然后用它把DESKeySpec转换成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            //Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("DES");
            //用密匙初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            //现在，获取数据并加密
            //正式执行加密操作
            return cipher.doFinal(datasource);
        }catch(Throwable e){
            logger.error("", e);
        }
        return null;
    }
    /**
     * 解密
     * @param src byte[]
     * @param password String
     * @return byte[]
     * @throws Exception
     */
    public static byte[] decrypt(byte[] src, String password) throws Exception {
        // DES算法要求有一个可信任的随机数源
                SecureRandom random = new SecureRandom();
        // 创建一个DESKeySpec对象
                DESKeySpec desKey = new DESKeySpec(password.getBytes());
        // 创建一个密匙工厂
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // 将DESKeySpec对象转换成SecretKey对象
                SecretKey securekey = keyFactory.generateSecret(desKey);
        // Cipher对象实际完成解密操作
                Cipher cipher = Cipher.getInstance("DES");
        // 用密匙初始化Cipher对象
                cipher.init(Cipher.DECRYPT_MODE, securekey, random);
        // 真正开始解密操作
                return cipher.doFinal(src);
    }
}
